<?php

namespace app\controller;

use app\BaseController;
use app\model\AdminModel;
use app\model\DormitoryModel;
use think\facade\Db;
use think\facade\Validate;
use think\Request;
use app\middleware\Check;
use think\response\Json;
use think\validate\ValidateRule;

/**
 * Class Storied 管理端 楼栋控制器
 * @package app\controller
 */
class Storied extends BaseController
{
    protected $middleware = [Check::class];

    /**宿舍列表
     * @param Request $request
     * @return Json
     */
    public function storied_list(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');
        $input['page'] = $request->post('page');
        $input['school_id'] = $request->post('school_id');
        $input['name'] = $request->post('name');
        $input['gender'] = $request->post('gender');
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => function($v) {
                return $v === null ? '缺少参数': true;
            },
            'gender' => function($v) {
                return in_array($v, ['', '0', '1', '2'], true) ? true : '缺少参数';
            }
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }

        // 逻辑处理
        return DormitoryModel::list($input, $request->post('token'));
    }

    /** 简单宿舍列表
     * @param Request $request
     */
    public function simple_list(Request $request){
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $data = Db::table('eb_dormitory')->field('id,name')->select()->toArray();
        return jsonSuccess('获取成功！',['data'=>$data]);
    }

    /**添加宿舍楼
     * @param Request $request
     * @return Json
     */
    public function increase(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['name'] = $request->param('name');// 宿舍楼名称
        $input['school_id'] = $request->param('school_id');//学校id
        $input['gender'] = $request->param('gender');//宿舍楼类型
        $input['manag_id'] = $request->param('manag_id');//宿舍楼类型
        // 参数校验
        $validate = Validate::rule([
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'manag_id' => ValidateRule::isRequire(null, '缺少参数'),
            'gender' => function ($value) {
                return in_array($value, ['0', '1', '2'], true)? true : '不合法的参数';
            }
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return DormitoryModel::increase($input, $request->post('token'));
    }

    /**删除宿舍
     * @param Request $request
     * @return Json
     */
    public function delete_storied(Request $request){
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['school_id'] = $request->post('school_id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return DormitoryModel::delete($input, $request->post('token'));
    }
    /**根据ID获取楼栋信息
     * @param Request $request
     * @return Json
     */
    public function storied_find(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['school_id'] = $request->post('school_id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return DormitoryModel::getOnly($input, $request->post('token'));
    }

    /**编辑保存楼栋
     * @param Request $request
     * @return Json
     */
    public function storied_edit(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['name'] = $request->post('name');
        $input['gender'] = $request->post('gender');
        $input['manag_id'] = $request->post('manag_id');
        $input['school_id'] = $request->post('school_id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'manag_id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'gender' => function ($value) {
                return in_array($value, ['0', '1', '2'], true)? true : '不合法的参数';
            }
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return DormitoryModel::edit($input, $request->post('token'));
    }



    /**获取管理下的学校
     * @param Request $request
     * @return Json
     */
    public function getOwnSchool(Request $request)
    {
        try{
            if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($request->post('token'), [1], [2]);
            if ($rtn instanceof Json) return $rtn;
            // 获取列表
            $table = Db::table('eb_school');
            $table->field('id, name');
            $map[] = ['operate_id', '=', $rtn['id']];
            $list = $table->where($map)->order('create_time', 'desc')->select();
            return jsonSuccess('查询成功', $list);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
}
